package Test.com.fjlzy.first;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Demo {
    public static void main(String[] args) throws ParseException {
        //5.创建集合对象，用于存储变量【光照】，【水分】，【温度】的数据----额外补充土壤酸碱度
        List<Temperature> temperaturesList = new ArrayList<>();

        List<Light> lightList = new ArrayList<>();

        List<Moisture> moistureList = new ArrayList<>();

        List<adjustEC> adjustECList = new ArrayList<>();


        //4.利用while循环完成再次返回到主界面
        while (true) {
            //1.用输出语句完成主界面的内容编写
            System.out.println("\n==========欢迎来到模拟环境============\n");
            System.out.println("\t\t\t1.检测温度");
            System.out.println("\t\t\t2.检测水分");
            System.out.println("\t\t\t3.检测光照");
            System.out.println("\t\t\t4.监测营养液的浓度");
            System.out.println("\t\t\t5.查看当前信息");
            System.out.println("\t\t\t6.退出");
            System.out.print("\t\t\t请输入序号进行选择功能:");

            //2.使用Scanner函数进行数据键盘录入
            Scanner sc = new Scanner(System.in);
            String line = sc.nextLine();


            //3.用Switch语句完成操作的选择
            switch (line) {
                case "1":
                    System.out.println("检测温度");
                    detectTemperature(temperaturesList);
                    break;

                case "2":
                    System.out.println("检测水分");
                    detectMoisture(moistureList);
                    break;

                case "3":
                    System.out.println("检测光照");
                    detectLight(lightList);
                    break;

                case "4":
                    System.out.println("自动监测营养液浓度值");
                    detectadjustEC(adjustECList);
                    break;

                case "5":
                    System.out.println("查看当前环境");
                    findAllTemperature(temperaturesList);
                    findAllMoisture(moistureList);
                    findAllLight(lightList);
                    findAdjustEC(adjustECList);
                    break;

                case "6":
                    System.out.println("谢谢使用");
                    System.exit(0);            //JVM退出

            }
        }
    }



    //1.定义方法，实现检测温度
    public static void detectTemperature(List<Temperature> temperatureList) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date begin = dateFormat.parse("2022-04-01");
        Date end = dateFormat.parse("2022-11-30");
        Boolean result = getDate(begin, end);

        Temperature t = new Temperature();

        //当前时间是4月1号到11月30号
        if (result) {
            //键盘录入温度
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入当前温度:");
            double temp = sc.nextDouble();
            t.setTemperature(temp);
            temperatureList.add(t);
            if (t.getTemperature() > 30) {              //温度高于30度
                //自动制冷空调
                System.out.println("启动制冷空调");
            } else if (t.getTemperature() < 20) {       //温度低于20度
                //关闭制冷空调
                System.out.println("关闭制冷空调");
            } else {
                System.out.println("当前温度适宜");
            }
        } else {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入当前温度:");
            double temp = sc.nextDouble();
            t.setTemperature(temp);
            if (t.getTemperature() < 10) {               //温度低于10度
                //启动制热空调
                System.out.println("启动制热空调");
            } else if (t.getTemperature() > 25) {       //温度高于25度
                //关闭制热空调
                System.out.println("关闭制热空调");
            } else {
                System.out.println("当前温度适宜");
            }
        }
    }

    //定义日期方法
    public static Boolean getDate(Date date1, Date date2) {
        boolean flag = false;
        //获取当前时间
        Calendar nowDate = Calendar.getInstance();
        nowDate.setTime(new Date());

        //获取开始时间
        Calendar begin = Calendar.getInstance();
        begin.setTime(date1);

        //获取结束时间
        Calendar end = Calendar.getInstance();
        end.setTime(date2);
        if ((nowDate.after(begin) && nowDate.before(end)) || (nowDate.getTime() == begin.getTime() || nowDate.getTime() == end.getTime())) {

           // System.out.println("获取当前日期在中间");
            flag = true;
        }
        return flag;

    }

    //定义方法，实现水分检测功能
        public static void detectMoisture(List<Moisture> moistureList){
            Moisture m = new Moisture();
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入当前水分:");
            double mois = sc.nextDouble();
            m.setMoisture(mois);
            moistureList.add(m);
            if (m.getMoisture() < 30) {
                System.out.println("开启补水3分钟");
            }else{
                System.out.println("当前水分适宜");
            }
        }

        //定义方法,实现检测光照功能
        public static void detectLight(List<Light> lightList) throws ParseException {
            DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
            Date begin = dateFormat.parse("03:00:00");
            Date end = dateFormat.parse("20:59:59");
            Boolean result = getTime(begin, end);
            Light l = new Light();
            lightList.add(l);
            //当前时间在03：00----20:59

            if (result) {
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入当前光照:");
                int light = sc.nextInt();
                l.setLight(light);
                if (l.getLight() >= 70000) {
                    System.out.println("开启外遮阳");
                } else if (l.getLight() >= 90000) {
                    System.out.println("开启内遮阳");
                } else if (l.getLight() < 20000) {
                    System.out.println("关闭遮阳");
                } else {
                    System.out.println("当前光照适宜");
                }
            } else {
                //当前时间在21:00--02:59
                System.out.println("开启夜间补光灯");
            }

        }

        //定义时间的方法
        public static Boolean getTime(Date date1, Date date2) {
        boolean flag = false;
        //获取当前时间，并且设置其时间格式。
        Calendar nowDate = Calendar.getInstance();
        int hour = nowDate.get(Calendar.HOUR_OF_DAY);
        int minute = nowDate.get(Calendar.MINUTE);
        int second = nowDate.get(Calendar.SECOND);
        Date date = new Date();
        date.setTime(hour);
        date.setTime(minute);
        date.setTime(second);
        nowDate.setTime(date);

        //获取开始时间
        Calendar begin = Calendar.getInstance();
        begin.setTime(date1);

        //获取结束时间
        Calendar end = Calendar.getInstance();
        end.setTime(date2);

        if ((nowDate.after(begin) && nowDate.before(end)) ||
                (nowDate.getTime() == begin.getTime() || nowDate.getTime() == end.getTime())) {
        //    System.out.println("当前时间在中间");
            flag = true;
        }
        return flag;
    }

    //6.查看温度信息
    public static void findAllTemperature(List<Temperature> temperatureList) {
        //6.1判断集合是否有数据，如果码云显示信息
        if (temperatureList.size()==0) {
            System.out.println("无信息，请添加信息再查询");

            //6.2为了让程序不在往下执行，给return
            return;
        }

        //6.3输入表头信息
        System.out.println("温度\t\t\t水分\t\t\t光照\t\t\t营养液浓度");

        //6.4查看温度度信息
        for (int i = 0; i < temperatureList.size(); i++) {
            Temperature t = temperatureList.get(i);
            System.out.print(t.getTemperature()+ "℃");
        }
    }

    //7.查看水分信息
    public static void findAllMoisture(List<Moisture> moistureList) {
        for (int i = 0; i < moistureList.size(); i++) {
            Moisture m = moistureList.get(i);
            System.out.print("\t\t" + m.getMoisture()+ "%");
        }
    }

    //8.查看光照信息
    public static void findAllLight(List<Light> lightList) {
        for (int i = 0; i < lightList.size(); i++) {
            Light l = lightList.get(i);
            System.out.print("\t\t" + l.getLight()+ "lux");

        }
    }

    //9.查看自动监测营养液浓度
    public static void findAdjustEC(List<adjustEC> adjustECList) {
        for (int i = 0; i < adjustECList.size(); i++) {
            adjustEC a1 = adjustECList.get(i);
            System.out.print("\t\t" + a1.getEc()+ "ec");

        }
    }

    //定义自动监测营养监测的方法
    public static void detectadjustEC(List<adjustEC> adjustECList) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date begin = dateFormat.parse("2022-03-01");
        Date end = dateFormat.parse("2022-7-1");
        Boolean result = getDate(begin, end);

        adjustEC a1 = new adjustEC();
        adjustECList.add(a1);

        //进行营养液区间的判断。
        if (result) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入当前营养液EC:");
            double adjustEC = sc.nextDouble();
            a1.setEc(adjustEC);
            if (a1.getEc() < 1.0) {
                System.out.println("启动补充营养液");
            } else  if (a1.getEc() > 1.6) {
                System.out.println("停止补充营养液");
            }
        } else {
        
            System.out.println("请检查营养液浓度是否正常。");
        }

        //设置日期从每年的9月1到12月3日。
        DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        Date begin1 = dateFormat.parse("2022-09-01");
        Date end1 = dateFormat.parse("2022-12-3");
        Boolean result1 = getDate(begin, end);

        if(result1){
           Scanner sc = new Scanner(System.in);
           double adjustEC = sc.nextDouble();
           a1.setEc(adjustEC);
           if (a1.getEc() < 0.8) {
                System.out.println("启动补充营养液");
            } else if (a1.getEc() > 1.5){
                System.out.println("停止补充营养液");
            }
        }
    }
}
